//
//  TOPlayButton.m
//  SeaGullFM
//
//  Created by dllo on 15/11/22.
//  Copyright © 2015年 tianou. All rights reserved.
//

#import "WSPlayButton.h"
#import "WSPlayViewController.h"

@implementation WSPlayButton

+(instancetype)shareButton
{
    static WSPlayButton *playButton = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        playButton = [[WSPlayButton alloc] init];
        [playButton createSubviews];
    });
    return playButton;
}

-(void)createSubviews
{
//    self.backgroundColor = CLEAR;
    self.frame =CGRectMake(SCREEN_WIDTH*0.4, -24, SCREEN_WIDTH*0.2, SCREEN_HEIGHT*0.2);
    self.userInteractionEnabled = YES;
    
    //背景
    self.btnBackground = [[UIImageView alloc]initWithFrame:self.bounds];
    self.btnBackground.image = [UIImage imageNamed:@"tabbar_np_normal"];
    self.btnBackground.layer.masksToBounds = YES;
    self.btnBackground.layer.cornerRadius = SCREEN_WIDTH*0.1;
    [self addSubview:self.btnBackground];
    self.btnBackground.tag = 1000;
    
    //条
    UIImageView *tiao = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH*0.2, SCREEN_WIDTH*0.2)];
    tiao.backgroundColor = CLEAR;
    tiao.image = [UIImage imageNamed:@"tabbar_np_loop"];
    tiao.center = CGPointMake(SCREEN_WIDTH*0.1, SCREEN_WIDTH*0.1);
    [self.btnBackground addSubview:tiao];
    
    //旋转ImageView
    self.musicBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.musicBtn.frame = CGRectMake(0, 0, SCREEN_WIDTH*0.15, SCREEN_WIDTH*0.15);
    self.musicBtn.backgroundColor = CLEAR;
//    [self.musicBtn sd_setImageWithURL:[[[TOFMData shareFMDB] selectTrackImageData] lastObject] forState:UIControlStateNormal placeholderImage:PLACEHOLDER_CELL_CIRCLE];
    self.musicBtn.layer.masksToBounds = YES;
    self.musicBtn.layer.cornerRadius = SCREEN_WIDTH*0.075;
    self.musicBtn.center = CGPointMake(SCREEN_WIDTH*0.1, SCREEN_WIDTH*0.1);
    [tiao addSubview:self.musicBtn];
    
    
    
    //播放按钮
    self.playBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.playBtn.backgroundColor = CLEAR;
    self.playBtn.frame = CGRectMake(0, 0, SCREEN_WIDTH*0.15, SCREEN_WIDTH*0.15);
    self.playBtn.layer.masksToBounds = YES;
    self.playBtn.layer.cornerRadius = SCREEN_WIDTH*0.075;
    self.playBtn.center = CGPointMake(SCREEN_WIDTH*0.1, SCREEN_WIDTH*0.1);
    [self.playBtn setImage:[UIImage imageNamed:@"tabbar_np_play@2x.png"] forState:UIControlStateNormal];
    self.playBtn.alpha = 0.6;
    [self.playBtn addTarget:self action:@selector(playMusic1:) forControlEvents:UIControlEventTouchUpInside];
    [tiao addSubview:self.playBtn];
    
    self.btnBackground.userInteractionEnabled = YES;
    tiao.userInteractionEnabled = YES;
    
}

-(void)playMusic1:(UIButton *)btn
{
    
    WSPlayViewController *player = [WSPlayViewController sharaMusicPlayer];
    UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:player];
    [player.player resume];
    [self.window.rootViewController presentViewController:naVC animated:YES completion:^{
        
        [self startCircle];
        
    }];
}

-(void)startCircle
{
    
#pragma mark - musicBtn旋转动画
    //改变view Z轴
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    
    //旋转的起始值 从零开始转
    animation.fromValue = [NSNumber numberWithInt:0];
    
    //最终旋转的角度是360°
    animation.toValue = [NSNumber numberWithInt:M_PI * 2];
    
    //旋转时长
    animation.duration = 5;
    
    //重复次数 0 为无限次
    animation.repeatCount = NSIntegerMax;
    
    //旋转结束后是否要逆向返回原位置
    animation.autoreverses = NO;
    
    //是否按照结束位置继续旋转
    animation.cumulative = YES;
    
    //给view的layer层添加动画,key是标记  随便起名字
    [self.musicBtn.layer addAnimation:animation forKey:@"circle"];
    NSLog(@"%f", self.musicBtn.layer.speed) ;
    self.musicBtn.layer.speed = 1.0;
    NSLog(@"%f", self.musicBtn.layer.speed) ;

    self.playBtn.alpha = 0;
    self.playBtn.enabled = NO;
}

-(void)stopCircle
{
    self.musicBtn.layer.speed = 0;
    NSLog(@"11111111");
    self.playBtn.alpha = 1;
    self.playBtn.enabled = YES;

}




/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
